package com.ucode.hrm.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjUtil;
import com.ucode.hrm.mapper.HrmAttendanceApplyRecordMapper;
import com.ucode.hrm.service.HrmAttendanceApplyRecordService;
import com.ucode.hrm.service.HrmAttendanceConfigService;
import com.ucode.hrm.service.HrmAttendanceRecordService;
import com.ucode.hrm.service.HrmEmployeeService;
import com.ucode.model.hrm.HrmAttendanceApplyRecord;
import com.ucode.model.hrm.HrmAttendanceConfig;
import com.ucode.model.hrm.HrmEmployee;
import com.ucode.model.hrm.enums.HrmClockType;
import com.ucode.tool.base.enums.CommonAuditStatusEnum;
import com.ucode.tool.base.enums.GlobalResultCodeEnum;
import com.ucode.tool.exception.UcodeServiceException;
import groovy.lang.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class HrmAttendanceApplyRecordServiceImpl implements HrmAttendanceApplyRecordService {

    @Resource
    private HrmAttendanceApplyRecordMapper attendanceApplyRecordMapper;
    @Resource
    @Lazy
    private HrmAttendanceRecordService hrmAttendanceRecordService;
    @Resource
    @Lazy
    private HrmAttendanceConfigService hrmAttendanceConfigService;
    @Resource
    @Lazy
    private HrmEmployeeService hrmEmployeeService;


    @Override
    public HrmAttendanceApplyRecordMapper initMapper() {
        return attendanceApplyRecordMapper;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(Long id, Integer auditStatus, Long processorId, String processRemark) {
        HrmAttendanceApplyRecord bean = attendanceApplyRecordMapper.selectByPrimaryKey(id);
        if(bean.getDeltag() || !ObjUtil.equals(bean.getAuditStatus(), CommonAuditStatusEnum.SUBMIT_REVIEW.getCode())) {
            return;
        }

        HrmAttendanceApplyRecord updateBean = new HrmAttendanceApplyRecord();
        updateBean.setId(id);
        updateBean.setAuditStatus(auditStatus);
        updateBean.setProcessRemark(processRemark);
        updateBean.setProcessorId(processorId);
        updateBean.setProcessTime(LocalDateTimeUtil.now());

        attendanceApplyRecordMapper.update(updateBean);

        if(auditStatus == CommonAuditStatusEnum.SUCCESS.getCode()) {
            HrmEmployee hrmEmployee = hrmEmployeeService.validate(bean.getEmployeeId());
            if(ObjUtil.isNull(hrmEmployee.getAttCfgId()) || ObjUtil.equals(hrmEmployee.getAttCfgId(), 0)){
                throw new UcodeServiceException(GlobalResultCodeEnum.VALIDATE_FAILED.getCode(), "员工未设置考勤配置");
            }
            HrmAttendanceConfig config = hrmAttendanceConfigService.validate(hrmEmployee.getAttCfgId());

            HrmClockType clockType = HrmClockType.valueOf(bean.getClockType());
            if(ObjUtil.isNull(clockType))
                throw new UcodeServiceException(GlobalResultCodeEnum.VALIDATE_FAILED.getCode(), "打卡类型错误");
            hrmAttendanceRecordService.reissueAttendance(config, bean.getEmployeeId(), bean.getCompanyId(), bean.getProjectId(), clockType, bean.getWorkDate());

        }
    }
}
